﻿using DevExpress.XtraEditors;
using EkStudio.com.bean.xml.BillChart;
using EkStudio.com.bean.xml.BillDictionary;
using EkStudio.com.bean.xml.BillModule;
using EkStudio.com.bean.xml.BillMultiple;
using EkStudio.com.bean.xml.RootInterface;
using EkStudio.com.bean.xml.RootInterface.Plugin;
using EkStudio.com.bean.xml.Unit;
using EkStudio.com.ui.form;
using EkStudio.com.ui.form.FieldDropSource;
using EkStudio.com.ui.unitbill.property.Event;
using EkStudio.Com.UI.Interface;
using System.Windows.Forms;

namespace EkStudio.com.ui.unitbill.property
{
    public partial class ModuleUiFieldProperty : XtraUserControl
    {

        private log4net.ILog logger = log4net.LogManager.GetLogger(typeof(ModuleUiFieldProperty));

        private ProjectRootPanel RootPanel = null;

        private ProjectUnitBillInterface UnitPanel = null;

        private BillFormatModule format = null;

        
        private BillUiFieldToProperty selectField = null;

        public event KEventHandler CaptionChanged = null;

        public event KEventHandler LinkerChange = null;

        public ModuleUiFieldProperty(BillFormatModule _bean,ProjectRootPanel root, ProjectUnitBillInterface unit)
        {
            InitializeComponent();
            format = _bean;
            RootPanel = root;
            UnitPanel = unit;
            InitProperty();
        }
        private void InitProperty()
        {
            //初始化下拉框内容
            RepositoryEditType.Items.AddRange(BeanUtil.EDITTYPE);//控件类型
            RepositoryComboType.Items.AddRange(BeanUtil.COMBOTYPE);//下拉框类型
            RepositoryMemoryList.Items.AddRange(BeanUtil.MEMORYTYPE);//自动记忆
            RepositoryEditAreaDefault.Items.AddRange(BeanUtil.DEFAULT_EDIT_LIST);//编辑状态控件的默认值
            RepositorySearchAreaDefault.Items.AddRange(BeanUtil.DEFAULT_SEARCH_LIST);//查询条件下控件的默认值
            RepositoryCharCase.Items.AddRange(BeanUtil.CHARCASE);//字符大小写
            RepositorySummaryType.Items.AddRange(BeanUtil.SUMMARY);//汇总类型
            RepositoryAlingment.Items.AddRange(BeanUtil.ALIGNMENT);//居中类型
            RepositorySearchType.Items.AddRange(BeanUtil.SEARCHTYPE);//查询类型

            //初始化属性编辑器的只读属性
            Field_01_Caption.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_02_Key.Properties.ReadOnly = UnitPanel.ReadOnly; 
            Field_03_EditType.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_04_ComboType.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_05_MultiSelect.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_06_DataType.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_07_Size.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_08_DataSource.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_09_FnNotNull.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_10_FnDistinct.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_11_DtFormat.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_12_Memory.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_13_Enable.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_14_Visible.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_15_ReadOnly.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_16_PasswordType.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_17_CharCase.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_18_DefaultData.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_19_DropStyle.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_20_EditAlignment.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_21_EditColumns.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_22_EditRows.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_23_EditNewLine.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_24_EditShInGrid.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_25_EditExpend.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_26_ViewAlignment.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_27_ViewFnWidth.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_28_ViewShInGrid.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_29_ViewSubject.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_30_ViewSummary.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_31_ViewShSearch.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_32_ViewSearchType.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_33_ViewDefaultData.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_34_ViewShowMail.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_35_ViewDicMenu.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_36_ImageMaxSize.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_37_ViewLinker.Properties.ReadOnly = UnitPanel.ReadOnly;

            if (format is BillDicModuleItem)
            {
                Field_35_ViewDicMenu.Visible = true;
            }else
            {
                Field_35_ViewDicMenu.Visible = false;
            }
        }

        public BillUiFieldToProperty SelectField
        {
            set
            {
                BillUiFieldToProperty oldField = selectField;
                selectField = value;
                if(PropertyGrid.SelectedObject == null)
                {
                    PropertyGrid.SelectedObject = selectField;
                }
                else
                {
                    BillUiFieldToProperty tg = PropertyGrid.SelectedObject as BillUiFieldToProperty;
                    
                    tg.setField(selectField);
                    PropertyGrid.Refresh();
                }
                if (oldField == null || oldField.EditType != selectField.EditType)
                {
                    EditAction(true);
                }
                else
                {
                    EditAction(false);
                }
            }
        }

        public BillUiFieldToProperty[] SelectFields
        {
            set
            {
                selectField = new BillUiFieldToProperty(value);
                if(PropertyGrid.SelectedObject == null)
                {
                    PropertyGrid.SelectedObject = selectField;
                }
                else
                {
                    BillUiFieldToProperty tg = PropertyGrid.SelectedObject as BillUiFieldToProperty;
                    tg.setField(selectField);
                    PropertyGrid.Refresh();
                }
            }
        }


        private void EditAction(bool change)
        {
            string value = selectField.EditType;
            //重新初始化数据类型下拉框内容
            RepositoryDataType.Items.Clear();
            RepositoryDataType.Items.AddRange(BeanUtil.getDataType(value));
            //重新初始化数据格式化下拉框内容
            RepositoryFormatList.Items.Clear();
            RepositoryFormatList.Items.AddRange(BeanUtil.getFormatType(value));
            #region 控件类型改变
            if (change)
            {
                switch (value)
                {
                    case BeanUtil.KT_TEXTFIELD_TYPE:
                        Field_04_ComboType.Visible = false;//下拉框类型
                        Field_05_MultiSelect.Visible = false;//下拉框是否允许多选
                        Field_19_DropStyle.Visible = false;//下拉框数据源设置
                        Field_11_DtFormat.Visible = true;
                        Field_12_Memory.Visible = true;
                        Field_36_ImageMaxSize.Visible = false;
                        break;
                    case BeanUtil.KT_MEMOFIELD_TYPE:
                        Field_04_ComboType.Visible = false;//下拉框类型

                        Field_05_MultiSelect.Visible = false;//下拉框是否允许多选

                        Field_19_DropStyle.Visible = false;//下拉框数据源设置

                        Field_11_DtFormat.Visible = true;

                        Field_12_Memory.Visible = false;
                        Field_36_ImageMaxSize.Visible = false;
                        break;
                    case BeanUtil.KT_COMBOBOX_TYPE:
                        Field_03_EditType.Expanded = true;
                        Field_04_ComboType.Visible = true;//下拉框类型
                        Field_05_MultiSelect.Visible = true;//下拉框是否允许多选
                        Field_19_DropStyle.Visible = true;//下拉框数据源设置
                        Field_05_MultiSelect.Visible = true;//下拉框是否允许多选

                        Field_11_DtFormat.Visible = true;//格式化
                        Field_12_Memory.Visible = false;
                        Field_36_ImageMaxSize.Visible = false;
                        break;
                    case BeanUtil.KT_COMBOBOXGRID_TYPE:
                        Field_03_EditType.Expanded = true;
                        Field_04_ComboType.Visible = true;//下拉框类型
                        Field_05_MultiSelect.Visible = true;//下拉框是否允许多选
                        Field_19_DropStyle.Visible = true;//下拉框数据源设置
                        Field_05_MultiSelect.Visible = true;//下拉框是否允许多选

                        Field_11_DtFormat.Visible = true;//格式化
                        Field_12_Memory.Visible = false;
                        Field_36_ImageMaxSize.Visible = false;
                        break;
                    case BeanUtil.KT_DATEFIELD_TYPE:

                        Field_04_ComboType.Visible = false;//下拉框类型
                        Field_05_MultiSelect.Visible = false;//下拉框是否允许多选

                        Field_19_DropStyle.Visible = false;//下拉框数据源设置

                        Field_11_DtFormat.Visible = true;

                        Field_12_Memory.Visible = false;
                        Field_36_ImageMaxSize.Visible = false;
                        break;
                    case BeanUtil.KT_BUTTON_TYPE:
                        Field_04_ComboType.Visible = false;//下拉框类型

                        Field_05_MultiSelect.Visible = false;//下拉框是否允许多选

                        Field_19_DropStyle.Visible = false;//下拉框数据源设置

                        Field_11_DtFormat.Visible = true;

                        Field_12_Memory.Visible = false;
                        Field_36_ImageMaxSize.Visible = false;
                        break;
                    case BeanUtil.KT_HYPERLINK_TYPE:
                        Field_04_ComboType.Visible = false;//下拉框类型

                        Field_05_MultiSelect.Visible = false;//下拉框是否允许多选

                        Field_19_DropStyle.Visible = false;//下拉框数据源设置

                        Field_11_DtFormat.Visible = true;

                        Field_12_Memory.Visible = false;
                        Field_36_ImageMaxSize.Visible = false;
                        break;
                    case BeanUtil.KT_IMAGEBUT_TYPE:
                        Field_04_ComboType.Visible = false;//下拉框类型

                        Field_05_MultiSelect.Visible = false;//下拉框是否允许多选
                        Field_19_DropStyle.Visible = false;//下拉框数据源设置
                        Field_11_DtFormat.Visible = true;

                        Field_12_Memory.Visible = false;
                        Field_36_ImageMaxSize.Visible = true;
                        break;
                    default: break;

                }
            }
            #endregion

            if (selectField.ViewShInGrid)
            {
                Field_26_ViewAlignment.Visible = true;
                Field_27_ViewFnWidth.Visible = true;
                Field_29_ViewSubject.Visible = true;
                Field_30_ViewSummary.Visible = true;
                Field_37_ViewLinker.Visible = true;
                Field_28_ViewShInGrid.Expanded = true;
            }
            else
            {
                Field_26_ViewAlignment.Visible = false;
                Field_27_ViewFnWidth.Visible = false;
                Field_29_ViewSubject.Visible = false;
                Field_30_ViewSummary.Visible = false;
                Field_37_ViewLinker.Visible = false;
            }

            if (selectField.IsExpendTable())//是自动列
            {
                Field_25_EditExpend.Visible = true;
            }
            else
            {
                Field_25_EditExpend.Visible = false;
            }

            

        }


        private void PropertyGrid_CellValueChanged(object sender, DevExpress.XtraVerticalGrid.Events.CellValueChangedEventArgs e)
        {
            if (UnitPanel.ReadOnly) return;
            bool ReLoad = false;
            string key = e.Row.Properties.FieldName;
            object objValue = e.Value;
            //RootPanel.AddConsoleText(key,false);
            if (key == "Caption") {
                if (this.CaptionChanged != null)
                {
                    PropertyDataEvent data = new PropertyDataEvent("", e.Value.ToString());
                    this.CaptionChanged(this, data);
                }
            }
            else if (key == "Key")//字段名验证
            {
                string value = objValue.ToString().ToLower();
                if (BeanUtil.PROTECT_FEILD_NAME.Contains(value))
                {
                    selectField.Key = BeanUtil.FIELD_KEY_VALUE;
                    RootPanel.AddConsoleText(string.Format("字段名[{0}为系统关键字，不可使用]", value), true);
                    RootPanel.ShowAlert("错误信息", string.Format("字段名[{0}为系统关键字，不可使用]", value));
                    ReLoad = true;
                }
                else
                {
                    ReLoad = true;
                    selectField.DataSource = value;
                }

            }
            else if (key == "EditType")
            {
                string value = objValue.ToString();
                //重新初始化数据类型下拉框内容
                RepositoryDataType.Items.Clear();
                RepositoryDataType.Items.AddRange(BeanUtil.getDataType(value));
                //重新初始化数据格式化下拉框内容
                RepositoryFormatList.Items.Clear();
                RepositoryFormatList.Items.AddRange(BeanUtil.getFormatType(value));

                switch (value)
                {
                    case BeanUtil.KT_TEXTFIELD_TYPE://----------------文本控件
                        selectField.DataType = BeanUtil.KT_DTSTRING;
                        Field_04_ComboType.Visible = false;//下拉框类型
                        selectField.ComboType = "";

                        Field_05_MultiSelect.Visible = false;//下拉框是否允许多选
                        selectField.MultiSelect = false;

                        Field_19_DropStyle.Visible = false;//下拉框数据源设置

                        Field_11_DtFormat.Visible = true;
                        selectField.DtFormat =  "";

                        Field_12_Memory.Visible = true;
                        selectField.Memory = BeanUtil.MEMORYTYPE[0];
                        Field_36_ImageMaxSize.Visible = false;
                        break;
                    case BeanUtil.KT_MEMOFIELD_TYPE://--------------------备注框控件
                        selectField.DataType = BeanUtil.KT_DTMEMO;

                        Field_04_ComboType.Visible = false;//下拉框类型
                        selectField.ComboType = "";

                        Field_05_MultiSelect.Visible = false;//下拉框是否允许多选
                        selectField.MultiSelect = false;

                        Field_19_DropStyle.Visible = false;//下拉框数据源设置
                        Field_11_DtFormat.Visible = true;
                        selectField.DtFormat = BeanUtil.KT_DTFORMAT_TEXT;

                        Field_12_Memory.Visible = false;
                        selectField.Memory = BeanUtil.MEMORYTYPE[0];
                        Field_36_ImageMaxSize.Visible = false;
                        break;
                    case BeanUtil.KT_COMBOBOX_TYPE://-----下拉框控件
                        selectField.DataType = BeanUtil.getDataType(value).Length > 0 ? BeanUtil.getDataType(value)[0] : "";
                        Field_04_ComboType.Visible = true;//下拉框类型
                        selectField.ComboType = BeanUtil.COMBOTYPE[0];
                        Field_05_MultiSelect.Visible = true;//下拉框是否允许多选
                        selectField.MultiSelect = false;

                        Field_19_DropStyle.Visible = true;//下拉框数据源设置


                        Field_05_MultiSelect.Visible = true;//下拉框是否允许多选
                        selectField.MultiSelect = false;

                        Field_11_DtFormat.Visible = false;//格式化
                        selectField.DtFormat = "";
                        Field_12_Memory.Visible = false;
                        selectField.Memory = "";
                        Field_36_ImageMaxSize.Visible = false;
                        break;
                    case BeanUtil.KT_COMBOBOXGRID_TYPE://-----下拉框控件
                        selectField.DataType = BeanUtil.getDataType(value).Length > 0 ? BeanUtil.getDataType(value)[0] : "";
                        Field_04_ComboType.Visible = true;//下拉框类型
                        selectField.ComboType = BeanUtil.COMBOTYPE[0];
                        Field_05_MultiSelect.Visible = true;//下拉框是否允许多选
                        selectField.MultiSelect = false;

                        Field_19_DropStyle.Visible = true;//下拉框数据源设置


                        Field_05_MultiSelect.Visible = true;//下拉框是否允许多选
                        selectField.MultiSelect = false;

                        Field_11_DtFormat.Visible = false;//格式化
                        selectField.DtFormat = "";
                        Field_12_Memory.Visible = false;
                        selectField.Memory = "";
                        Field_36_ImageMaxSize.Visible = false;
                        break;
                    case BeanUtil.KT_DATEFIELD_TYPE://-------------日期控件
                        selectField.DataType = BeanUtil.KT_DTDATETIME;
                        Field_04_ComboType.Visible = false;//下拉框类型
                        selectField.ComboType = "";

                        Field_05_MultiSelect.Visible = false;//下拉框是否允许多选
                        selectField.MultiSelect = false;

                        Field_19_DropStyle.Visible = false;//下拉框数据源设置

                        Field_11_DtFormat.Visible = true;
                        selectField.DtFormat = BeanUtil.getFormatType(value).Length > 0 ? BeanUtil.getFormatType(value)[0] : "";

                        Field_12_Memory.Visible = false;
                        selectField.Memory = BeanUtil.MEMORYTYPE[0];
                        Field_36_ImageMaxSize.Visible = false;
                        break;
                    case BeanUtil.KT_BUTTON_TYPE://按钮控件
                        selectField.DataType = BeanUtil.KT_DTSTRING;
                        selectField.DataSource = "";

                        Field_04_ComboType.Visible = false;//下拉框类型
                        selectField.ComboType = "";

                        Field_05_MultiSelect.Visible = false;//下拉框是否允许多选
                        selectField.MultiSelect = false;

                        Field_19_DropStyle.Visible = false;//下拉框数据源设置

                        Field_11_DtFormat.Visible = false;
                        selectField.DtFormat = BeanUtil.getFormatType(value).Length > 0 ? BeanUtil.getFormatType(value)[0] : "";

                        Field_12_Memory.Visible = false;
                        selectField.Memory = BeanUtil.MEMORYTYPE[0];
                        Field_36_ImageMaxSize.Visible = false;
                        break;
                    case BeanUtil.KT_HYPERLINK_TYPE://按钮控件
                        selectField.DataType = BeanUtil.KT_DTDATETIME;
                        selectField.DataSource = "";

                        Field_04_ComboType.Visible = false;//下拉框类型
                        selectField.ComboType = "";

                        Field_05_MultiSelect.Visible = false;//下拉框是否允许多选
                        selectField.MultiSelect = false;

                        Field_19_DropStyle.Visible = false;//下拉框数据源设置

                        Field_11_DtFormat.Visible = true;
                        selectField.DtFormat = BeanUtil.getFormatType(value).Length > 0 ? BeanUtil.getFormatType(value)[0] : "";

                        Field_12_Memory.Visible = false;
                        selectField.Memory = BeanUtil.MEMORYTYPE[0];
                        Field_36_ImageMaxSize.Visible = false;
                        break;
                    case BeanUtil.KT_IMAGEBUT_TYPE://-----------------图片控件
                        selectField.DataType = BeanUtil.KT_DTIMAGE;

                        Field_04_ComboType.Visible = false;//下拉框类型
                        selectField.ComboType = "";

                        Field_05_MultiSelect.Visible = false;//下拉框是否允许多选
                        selectField.MultiSelect = false;

                        Field_19_DropStyle.Visible = false;//下拉框数据源设置

                        Field_11_DtFormat.Visible = true;
                        selectField.DtFormat = BeanUtil.getFormatType(value).Length > 0 ? BeanUtil.getFormatType(value)[0] : "";

                        Field_12_Memory.Visible = false;
                        selectField.Memory = BeanUtil.MEMORYTYPE[0];
                        Field_36_ImageMaxSize.Visible = true;
                        break;
                    default:break;
                }

                ReLoad = true;
            }
            else if (key == "ViewShInGrid")
            {
                //Field_28_ViewShInGrid
                if (e.Value.Equals(true))
                {
                    Field_26_ViewAlignment.Visible = true;
                    Field_27_ViewFnWidth.Visible = true;
                    Field_29_ViewSubject.Visible = true;
                    Field_30_ViewSummary.Visible = true;
                    Field_37_ViewLinker.Visible = true;
                }
                else
                {
                    Field_26_ViewAlignment.Visible = false;
                    Field_27_ViewFnWidth.Visible = false;
                    Field_29_ViewSubject.Visible = false;
                    Field_30_ViewSummary.Visible = false;
                    Field_37_ViewLinker.Visible = false;
                }
            }
            else if (key == "ViewShSearch")
            {
                if (e.Value.Equals(true))
                {
                    Field_32_ViewSearchType.Visible = true;
                    Field_33_ViewDefaultData.Visible = true;
                }
                else
                {
                    Field_32_ViewSearchType.Visible = false;
                    Field_33_ViewDefaultData.Visible = false;
                }
            }

            if (ReLoad)
            {
                this.PropertyGrid.Refresh();
            }
            UnitPanel.SaveStatus = false;

        }
        /// <summary>
        /// 下拉框选择字段
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RepositoryDropStyle_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (typeof(BillModuleItem) == format.GetType() 
                || typeof(BillMultipleItem) == format.GetType() 
                || typeof(BillDicModuleItem) == format.GetType()) 
            {
                AutoDropStyle DropStyle = new AutoDropStyle(RootPanel, format, UnitPanel);
                DropStyle.SetSelectField(selectField.FieldItem, null);
                DialogResult DR = DropStyle.ShowDialog();
                if(DR == DialogResult.OK)
                {
                    this.PropertyGrid.Refresh();
                }
            }
        }
        /// <summary>
        /// 链接框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RepositoryLinkerEdit_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (selectField.FieldItem != null&&(typeof(BillModuleItem) == format.GetType()||typeof(BillChartModuleItem) == format.GetType()))
            {
                if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Ellipsis)
                {
                    BillExpandCodeRoot root = null;
                    if (typeof(BillModuleItem) == format.GetType())
                    {
                        BillModuleItem it = (BillModuleItem)format;
                        root = it.ExpandCode;
                    }
                    if(typeof(BillChartModuleItem) == format.GetType())
                    {
                        BillChartModuleItem it = (BillChartModuleItem)format;
                        root = it.ExpandCode;
                    }
                    if (root != null)
                    {
                        FieldItemLinkerForm linkForm = new FieldItemLinkerForm(format,selectField.FieldItem, RootPanel,root, UnitPanel);
                        if (linkForm.ShowDialog() == DialogResult.OK)
                        {
                            if (LinkerChange != null)
                            {
                                PropertyDataEvent data = new PropertyDataEvent("", "");
                                LinkerChange(this, data);
                            }

                            UnitPanel.SaveStatus = false;
                            PropertyGrid.Refresh();
                        }
                    }
                    
                }
                else if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Delete)
                {
                    if(XtraMessageBox.Show("您确认删除链接吗","警告",MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        selectField.FieldItem.ViewBorder.Linker = null;
                        UnitPanel.SaveStatus = false;
                        PropertyGrid.Refresh();
                    }
                }

                
            }
            
        }
    }
}
